package com.ruoyi.project.knowledge.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.DataScope;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.common.utils.SnowflakeIdWorker;
import com.ruoyi.project.knowledge.domain.KnowledgeAttach;
import com.ruoyi.project.knowledge.domain.KnowledgeBase;
import com.ruoyi.project.knowledge.mapper.KnowledgeAttachMapper;
import com.ruoyi.project.knowledge.mapper.KnowledgeBaseMapper;
import com.ruoyi.project.knowledge.service.IKnowledgeBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class KnowledgeBaseServiceImpl implements IKnowledgeBaseService {
    @Autowired
    private KnowledgeBaseMapper knowledgeMapper;
    @Autowired
    private KnowledgeAttachMapper attachMapper;

    @Override
    @DataScope(deptAlias = "u1", userAlias = "u1")
    public List<KnowledgeBase> selectKnowledgeList(KnowledgeBase knowledge) {
        return knowledgeMapper.selectKnowledgeList(knowledge);
    }

    @Override
    public List<KnowledgeBase> selectPublicKnowledgeList(KnowledgeBase knowledge) {
        knowledge.setIsPublic("1");
        return knowledgeMapper.selectKnowledgeList(knowledge);
    }

    @Override
    public List<KnowledgeBase> searchKnowledgeList(String searchValue) {
        List<KnowledgeBase> list = knowledgeMapper.searchKnowledgeList(searchValue);
//        if(list != null && list.size() > 0){
//            for(KnowledgeBase k : list){
//                String content = k.getContent();
//            }
//        }

        return list;
    }

    @Override
    public KnowledgeBase selectKnowledgeById(String id) {
        KnowledgeBase knowledgeBase = knowledgeMapper.selectKnowledgeById(id);
        List<KnowledgeAttach> attachList = attachMapper.selectAttachByKnowledgeId(id);
        knowledgeBase.setAttachList(attachList);
        return knowledgeBase;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertKnowledge(KnowledgeBase knowledge, List<KnowledgeAttach> attachList) {
        if(knowledgeMapper.checkKnowledgeExist(knowledge) > 0){
            return AjaxResult.error("该标题已存在！");
        }
        knowledge.setId(SnowflakeIdWorker.generateId().toString());
        knowledge.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        knowledge.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId().toString());

        if(attachList != null && attachList.size() > 0) {
            for(KnowledgeAttach attach : attachList) {
                attach.setKnowledgeId(knowledge.getId());
                attachMapper.insertKnowledgeAttach(attach);
            }
        }

        return AjaxResult.success(knowledgeMapper.insertKnowledge(knowledge));
    }

    @Override
    public AjaxResult updateKnowledge(KnowledgeBase knowledge, List<KnowledgeAttach> attachList) {
        if(knowledgeMapper.checkKnowledgeExist(knowledge) > 0){
            return AjaxResult.error("该标题已存在！");
        }
        knowledge.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId().toString());

        if(attachList != null && attachList.size() > 0) {
            for(KnowledgeAttach attach : attachList) {
                attach.setKnowledgeId(knowledge.getId());
                attachMapper.insertKnowledgeAttach(attach);
            }
        }

        return AjaxResult.success(knowledgeMapper.updateKnowledge(knowledge));
    }

    @Override
    public int deleteKnowledge(String id) {
        return knowledgeMapper.deleteKnowledge(id);
    }
}
